import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {toJS} from 'mobx';
import {Button, Modal} from 'antd';
import {WeaLocaleProvider, WeaShare} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

@inject('myFavouriteStore', 'sysFavouriteStore', 'favouriteDialogStore', 'favouriteBrowserStore', 'sysFavFormStore')
@observer
export default class ItemBtns extends Component {
    constructor(props) {
        super(props);
        this.state = {
            shareShow: false,
            sharedata: {},
        };
    }

    handleMouseOver = (itemid, attr) => {
        const {sysFavouriteStore} = this.props;
        sysFavouriteStore.setBtnOver(itemid, attr, true);
    }

    handleMouseOut = (itemid, attr) => {
        const {sysFavouriteStore} = this.props;
        sysFavouriteStore.setBtnOver(itemid, attr, false);
    }

    /**
     * 转发按钮的移入移出
     */
    onRepeatOver = () => {
        const itemid = this.props.favid;
        const attr = 'isrepeatover';
        this.handleMouseOver(itemid, attr);
    }
    onRepeatOut = () => {
        const itemid = this.props.favid;
        const attr = 'isrepeatover';
        this.handleMouseOut(itemid, attr);
    }
    onRepeatClick = () => {
        const dataid = this.props.favid;
        const data = toJS(this.props.sysFavouriteStore.data);
        const shareTF = ['', 'doc', 'workflow'];
        let sharedata = {};
        data.map((value, index) => {
            if (value.favid === dataid) {
                sharedata = value;
                sharedata.type = shareTF[Number(value.favouritetype)];
            }
        });
        console.log(sharedata);
        // 这里遍历数据找到要的信息填充到分享页面
        this.props.sysFavouriteStore.sharedata = sharedata;
        this.props.sysFavouriteStore.shareShow = true;
    }

    /**
     * 编辑按钮的移入、移出、点击
     */
    onEidtOver = () => {
        const itemid = this.props.favid;
        const attr = 'iseditover';
        this.handleMouseOver(itemid, attr);
    }
    onEditOut = () => {
        const itemid = this.props.favid;
        const attr = 'iseditover';
        this.handleMouseOut(itemid, attr);
    }
    onEditClick = () => {
        const dataid = this.props.favid;
        const {sysFavFormStore, favouriteDialogStore} = this.props;
        sysFavFormStore.loadForm(dataid); // 加载编辑窗口的数据
        // 打开编辑窗口
        favouriteDialogStore.openEditDialog();
    }

    /**
     * 移动按钮的移入、移出、点击
     */
    onMoveOver = () => {
        const itemid = this.props.favid;
        const attr = 'ismoveover';
        this.handleMouseOver(itemid, attr);
    }
    onMoveOut = () => {
        const itemid = this.props.favid;
        const attr = 'ismoveover';
        this.handleMouseOut(itemid, attr);
    }
    onMoveClick = () => {
        const dataid = this.props.favid;
        const {sysFavouriteStore, favouriteDialogStore, favouriteBrowserStore} = this.props;

        // 设置需要移动的数据
        sysFavouriteStore.setMoveIds([dataid]);
        // 先清空数据
        favouriteBrowserStore.destroyBrowser();
        // 显示窗口
        favouriteDialogStore.openBrowserDialog();
        // 显示加载中的遮罩
        favouriteBrowserStore.toggleSpinning(true, `${getLabel(82275, '数据加载中，请稍候...')}`);
        // 加载目录树的数据
        favouriteBrowserStore.loadTreedata('0');
    }

    /**
     * 删除按钮的移入、移出、点击
     */
    onDeleteOver = () => {
        const itemid = this.props.favid;
        const attr = 'isdeleteover';
        this.handleMouseOver(itemid, attr);
    }
    onDeleteOut = () => {
        const itemid = this.props.favid;
        const attr = 'isdeleteover';
        this.handleMouseOut(itemid, attr);
    }
    onDeleteClick = () => {
        const itemid = this.props.favid;
        const {sysFavouriteStore} = this.props;
        confirm({
            content: `${getLabel(388215, '删除后不可恢复，您确认删除该条记录吗？')}`,
            onOk() {
                sysFavouriteStore.deleteFavourite(itemid);
            },
            onCancel() {
            },
        });
    }


    renderBtns = (ecId = '') => {
        const btns = [];
        const {myFavouriteStore} = this.props
        const {repeatIconTitle, editIconTitle, moveIconTitle, deleteIconTitle} = myFavouriteStore;

        const {favouritetype} = this.props;

        let repeatCls = '';
        let editCls = '';
        let moveCls = '';
        let deleteCls = '';
        const forwardCls = '';

        if (favouritetype == 6) { // 消息类型，不可编辑
            // btns.push(<span className='forward' title={forwardIconTitle} onMouseOver={this.onRepeatOver}
            //     onMouseOut={this.onRepeatOut} onClick={()=>{this.onRepeatClick()}}>
            //     <a href='javascript:void(0)' className={repeatCls}></a></span>);   //转发
            btns.push(
                <span
                    className="move"
                    title={moveIconTitle}
                    // onMouseOver={this.onMoveOver}
                    // onMouseOut={this.onMoveOut}
                    onClick={this.onMoveClick}
                    ecId={`${ecId}@move`}
                >
                     <a href="javascript:void(0)" className={moveCls}/>
                </span>
            ); // 移动

            btns.push(
                <span
                    className="delete"
                    title={deleteIconTitle}
                    // onMouseOver={this.onDeleteOver}
                    // onMouseOut={this.onDeleteOut}
                    onClick={this.onDeleteClick}
                    ecId={`${ecId}@delete`}
                >
                    <a href="javascript:void(0)" className={deleteCls}/>
                </span>
            ); // 删除
        } else {
            // btns.push(<span className='forward' title={forwardIconTitle} onMouseOver={this.onRepeatOver}
            //     onMouseOut={this.onRepeatOut} onClick={()=>{this.onRepeatClick()}}>
            //     <a href='javascript:void(0)' className={repeatCls}></a></span>);   //转发
            btns.push(
                <span
                    className="edit"
                    title={editIconTitle}
                    // onMouseOver={this.onEidtOver}
                    // onMouseOut={this.onEditOut}
                    onClick={this.onEditClick}
                    ecId={`${ecId}@edit`}
                >
                    <a href="javascript:void(0)" className={editCls}/>
                </span>
            ); // 编辑

            btns.push(
                <span
                    className="move"
                    title={moveIconTitle}
                    // onMouseOver={this.onMoveOver}
                    // onMouseOut={this.onMoveOut}
                    onClick={this.onMoveClick}
                    ecId={`${ecId}@move`}
                >
                    <a href="javascript:void(0)" className={moveCls}/>
                </span>
            ); // 移动

            btns.push(
                <span
                    className="delete"
                    title={deleteIconTitle}
                    // onMouseOver={this.onDeleteOver}
                    // onMouseOut={this.onDeleteOut}
                    onClick={this.onDeleteClick}
                    ecId={`${ecId}@delete`}
                >
                    <a href="javascript:void(0)" className={deleteCls}/>
                </span>
            ); // 删除
        }
        return btns;
    }


    render() {
        const {isover, ecId} = this.props;
        const btns = this.renderBtns(ecId);
        let cls = '';

        return (
            <div
                className={`sysfav-list-item-info-btns ${cls}`}
                ecId={`${this && this.props && this.props.ecId || ''}`}
            >
                {btns}
            </div>
        );
    }
}
